# -*- coding: utf-8 -*-


#books views
from models import Workshop, Category, Item, Unit ,Client
from django.http import HttpResponse
from django.core import serializers
from django.shortcuts import render_to_response as defrender_to_response, get_object_or_404
from django.utils import simplejson

def render_to_response(template, params):
    return defrender_to_response(template, params, mimetype = 'application/vnd.mozilla.xul+xml')

def ajax_response(data, status = 'true'):
   return HttpResponse(simplejson.dumps([{'status': status, 'data': data }]), mimetype='text/javascript' )

# Хрень какаято !!! Унифицировать! 
def category_list(request):
    items = Category.objects.all().order_by('name')
    workshops = Workshop.objects.all().order_by('name')
    return render_to_response('categories.xul', {'category_list': items, 'workshops_list': workshops})

def workshops_list(request):
        
    workshops = Workshop.objects.all().order_by('name')
    return render_to_response('workshops.xul', {'workshops_list': workshops})
    

def item_list(request):
        
    items = Item.objects.all().order_by('name')
    workshops = Workshop.objects.all().order_by('name')
    categories = Category.objects.all().order_by('name')
    units = Unit.objects.all().order_by('name')
    return render_to_response('items.xul', {'items_list': items,
                                            'workshops_list': workshops,
                                            'category_list': categories,
                                            'units_list': units})


def workshops_get(request, workshop_id):

    item = Workshop.objects.get(pk=workshop_id);
    return ajax_response({'id': item.id, 'name': item.name, 'description': item.description })

def category_get(request, id):
    item = Category.objects.get(pk = id );  
    return ajax_response({'id': item.id, 'name': item.name, 'related_item1': item.workshop.id })

def item_get(request, id):
    item = Item.objects.get(pk = id )
    width = item.width
    if item.width:
        width = float(item.width)
        
    return ajax_response({'id': item.id, 'name': item.__unicode__(), 'width': width, 'price1': float(item.price1) })

def client_get(request, id):
    item = Client.objects.get(pk = id )
    return ajax_response({'id': item.id, 'name': item.__unicode__()})

def category_save(request):
    #try: 
        if(int(request.POST['id'])>0):
            item = Category.objects.get(pk=int(request.POST['id']));   
            item.name = request.POST['name']
            workshop = Workshop.objects.get(pk=int(request.POST['related_item1']))
            item.workshop = workshop
            item.save()
            return ajax_response({'id': item.id, 'name': item.name, 'related_item1': workshop.id })
        
        item = Category()
        item.name = request.POST['name']
        workshop = Workshop.objects.get(pk=int(request.POST['related_item1']))
        item.workshop = workshop
        item.save()
        
        return ajax_response({'id': item.id, 'name': item.name, 'workshop_id': workshop.id })
    #except:
   #   return ajax_response(status = 'false', data={});

def workshops_save(request):
    try: 
        if(int(request.POST['id'])>0):
            item = Workshop.objects.get(pk=int(request.POST['id']));   
            item.name = request.POST['name']
            item.description = request.POST['description']
            item.save()
            return ajax_response({'id': item.id, 'name': item.name, 'description': item.description })
        item = Workshop()
        item.name = request.POST['name']
        item.description = request.POST['description']
        item.save()
        return ajax_response({'id': item.id, 'name': item.name, 'description': item.description })
    except:
      return ajax_response(status = 'false', data={});


    
def workshops_delete(request):
    try: 
        if(int(request.POST['id'])>0):
            item = Workshop.objects.get(pk=int(request.POST['id']));
            item.delete()
            return ajax_response(data = {'test': 'test'})
        return ajax_response(data = {}, status = 'false')
    except:
        return ajax_response(data = [{}], status = 'false')
    
